package com.tarzan.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tarzan.edu.dao.EduSubjectMapper;
import com.tarzan.edu.entity.EduSubject;
import com.tarzan.edu.entity.request.EduSubjectReq;
import com.tarzan.edu.entity.response.EduSubjectResp;
import com.tarzan.edu.entity.response.TreeEduSubject;
import com.tarzan.edu.service.IEduSubjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liujinshan
 * @title: EduSubjectServiceImpl
 * @projectName com.tarzan.edu.service.impl
 * @description: TODO
 * @date 2021/5/25 16:55
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements IEduSubjectService{
    /**
     * @param eduSubject
     * @return com.tarzan.common.core.base.BaseResponse
     * @throws
     * @title insert
     * @description TODO 新增课程分类信息
     * @author liujinshan
     * @data 2021/5/21 15:52
     */
    @Override
    public void insert(EduSubject eduSubject) {

    }

    /**
     * @param eduSubject
     * @return com.tarzan.common.core.base.BaseResponse
     * @throws
     * @title update
     * @description TODO 修改课程分类信息
     * @author liujinshan
     * @data 2021/5/21 15:59
     */
    @Override
    public void update(EduSubject eduSubject) {

    }

    /**
     * @param id
     * @return com.tarzan.common.core.base.BaseResponse
     * @throws
     * @title delete
     * @description TODO 删除课程分类信息
     * @author liujinshan
     * @data 2021/5/21 16:01
     */
    @Override
    public void delete(String id) {

    }

    /**
     * @param eduSubjectReq
     * @return com.tarzan.common.core.base.BaseResponse
     * @throws
     * @description 获取课程分类信息列表并分页
     * @author liujinshan
     * param eduTeacherReq
     * @date 2021-5-23 14:43
     */
    @Override
    public IPage<EduSubjectResp> findSubjectPageInfo(EduSubjectReq eduSubjectReq) {
        return null;
    }

    /**
     * @param id
     * @return
     * @throws
     * @description 根据id查询课程分类信息相细信息
     * @author liujinshan
     * param id
     * @date 2021-5-23 14:51
     */
    @Override
    public EduSubjectResp findSubjectById(String id) {
        return null;
    }

    /**
     * @return com.tarzan.edu.entity.response.TreeEduSubject
     * @title findTreeEduSubject
     * @description TODO
     * @author liujinshan
     * @data 2021/6/18 16:49
     */
    @Override
    public List<TreeEduSubject> findTreeEduSubject() {
        //1 查询所有一级分类  parentid = 0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        //2 查询所有二级分类  parentid != 0
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        List<TreeEduSubject> treeEduSubjects = new ArrayList<>();

        for (EduSubject eduSubject: oneSubjectList) {
            TreeEduSubject treeEduSubject = new TreeEduSubject();
            BeanUtils.copyProperties(eduSubject,treeEduSubject);
            treeEduSubjects.add(treeEduSubject);
            List<TreeEduSubject> treeEduSubjecttwoList = new ArrayList<>();
            //遍历二级分类list集合
            for (int m = 0; m < twoSubjectList.size(); m++) {
                //获取每个二级分类
                EduSubject tSubject = twoSubjectList.get(m);
                //判断二级分类parentid和一级分类id是否一样
                if(tSubject.getParentId().equals(eduSubject.getId())) {
                    //把tSubject值复制到TwoSubject里面，放到twoFinalSubjectList里面
                    TreeEduSubject treeEduSubjecttwo = new TreeEduSubject();
                    BeanUtils.copyProperties(tSubject,treeEduSubjecttwo);
                    treeEduSubjecttwoList.add(treeEduSubjecttwo);
                }
            }
            treeEduSubject.setChildren(treeEduSubjecttwoList);
        }

        return treeEduSubjects;
    }
}
